package gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

import parsing.InvalidSyntaxException;

import world.CritterWorld;
import world.LoadState;

public class LoadWorldFrame extends JFrame {
	
	private MainWindow mainFrame;
	
	private JPanel containerPanel;
	private JTextField worldText;
	private JTextField constantsText;
	private Box buttonBox;
	private JButton loadButton;
	private JButton cancelButton;

	private final Color bkg = new Color(255,99,71);

	public LoadWorldFrame(MainWindow m) {
		mainFrame = m;
		
		setSize(300, 200);
		setTitle("Load a world from file");
		containerPanel = new JPanel();
		containerPanel.setLayout(new GridLayout(0,1));
		containerPanel.setBorder(new LineBorder(bkg, 3));
		add(containerPanel);
		
		containerPanel.add(new JLabel("File to load new world from:", SwingConstants.LEFT));
		worldText = new JTextField("world.txt");
		containerPanel.add(worldText);
		containerPanel.add(new JLabel("World constants file:"));
		constantsText = new JTextField("constants.txt");
		containerPanel.add(constantsText);
		
		buttonBox = new Box(BoxLayout.X_AXIS);
		containerPanel.add(buttonBox);
		loadButton = new JButton("OK");
		loadButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				CritterWorld newWorld;
				try {
					LoadState state = new LoadState();
					newWorld = new CritterWorld(constantsText.getText(), worldText.getText(), state);
					if(!state.successful())
						JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(loadButton),
								"The world was sucessfully loaded but not every component was added" +
							  "\nbecause one or more was in an illegal location.", "Warning", JOptionPane.WARNING_MESSAGE);
					mainFrame.changeWorld(newWorld);
					mainFrame.repaint();
					SwingUtilities.getWindowAncestor(loadButton).dispose();
				} catch (InvalidSyntaxException e1) {
					JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(loadButton),
							e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
				} catch (IllegalArgumentException e1) {
					JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(loadButton),
							e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(SwingUtilities.getWindowAncestor(loadButton),
							e1.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		cancelButton = new CancelButton();
		buttonBox.add(loadButton);
		buttonBox.add(Box.createHorizontalStrut(20));
		buttonBox.add(cancelButton);
		
		containerPanel.setBackground(bkg);
		setVisible(true);
	}
	
}
